/**拆分类型列表,按字符块拆分
 * @param word 单个描述词*/
export function matchFormula(word) {
	if (isEmpty(word)) {
		return null
	}
	let REPLACE_KEY = "TTTTTTTT";
	let REPLACE_KEY_WEIGHT = "WWWWWWWW";
	let word2 = "";
	let weight = null;
	let list = transferToKindList(word)
	console.log("拆分后的列表：", list)
	let lora = false;

	let transfer = {
		origin: word,
		list: list,
		weight: weight,
		isModel: lora
	}
	return transfer;
}

/**
 *操作描述词的权重值
 * @param word 单个描述词
 * @param operate 1.加权重 2.减权重
 * 
 */
export function operateWordWeight(word, operate) {
	word = replaceCharaterToComma(word);
	let REPLACE_KEY = "TTTTTTTT";
	let REPLACE_KEY_WEIGHT = "WWWWWWWW";
	let transfer = getPromptTransfer(word)

	// console.log(transfer)
	let result = ""
	if (transfer.weight == null) { //原本没有权重值
		result = transfer.origin
		result = replaceAll(result, "(", "")
		result = replaceAll(result, ")", "")
		result = replaceAll(result, "{", "")
		result = replaceAll(result, "}", "")
		result = replaceAll(result, "【", "")
		result = replaceAll(result, "】", "")

		let changeWeight = 1
		if (operate == 1) {
			changeWeight += 0.1
		} else {
			changeWeight -= 0.1
		}
		result = "(" + result + ":" + changeWeight + ")";
	} else {
		for (var i = 0; i < transfer.list.length; i++) {
			if (transfer.list[i] == REPLACE_KEY_WEIGHT) { //如果是权重值的话就直接操作增减
				let weight = 1
				if (operate == 1) {
					weight = parseFloat((Number(transfer.weight) + 0.1).toFixed(2))
				} else {
					weight = parseFloat((Number(transfer.weight) - 0.1).toFixed(2))
				}
				if (weight > 1.6) {
					weight = 1.6
				}
				if (weight < 0.1) {
					weight = 0.1
				}
				result += weight
			} else if (transfer.list[i] == REPLACE_KEY) { //如果是权重值的话就直接操作增减

				result += transfer.word
			} else {
				result += transfer.list[i]
			}
		}
	}

	return result
}

/**拆分类型列表,按字符块拆分
 * @param word 单个描述词*/
export function getPromptTransfer(word) {
	if (word == null || word == undefined) {
		return {
			list: [],
			origin: "", //原始值
			word: "", //处理后的单独的词不含权重
			weight: 0, //权重
			isModel: false
		}
	}
	word = replaceCharaterToComma(word);
	word = word.trim()
	let REPLACE_KEY = "TTTTTTTT";
	let REPLACE_KEY_WEIGHT = "WWWWWWWW";
	let weight = null;
	let lora = false;
	let word2 = ""

	let list = []
	let isModel = false //是否为模型
	let lastType = 0; //0.未判断 1.文本 2.冒号 3.数字 4.括号起始符号
	for (var i = 0; i < word.length; i++) {
		let c = word[i]
		// if (isEmpty(c)) {
		// 	continue
		// }

		let type = 0

		if (c == ":") { //权重冒号
			type = 2
		} else if (isText(c)) {
			type = 1
		} else if (isNumeric(c)) { //数字的点也认为是数字类型而不是符号
			type = 3
		} else if (c == "(" || c == "{" || c == "[" || c == "<" || c == ")" || c == "}" || c == "]" || c == ">") {
			type = 4
		}

		console.log("当前遍历：", c, type)

		if (type == 0) {
			list.push(c)
			continue
		}

		if (type == 1 || type == 3) { //文本的话找到下一个结束的符号
			let content = ""
			while (true) {
				let w = word[i] //新提取出来的词
				if (w == ":" || w == ")" || w == "]" || w == "}") { //中断符号
					i--; //中断后后退一位便于后续计算判断
					break
				} else {
					content += word[i]
				}
				if (i + 1 >= word.length) {
					break;
				}
				i++
			}

			//先做特殊判断
			if (content == "lora" || content == "lyco") { //是模型
				list.push(content) //添加中断
				i++
				isModel = true
				// console.log(word[i])
				if (word[i] == ":") { //冒充先添加一个
					list.push(word[i]) //添加中断 :	
					i++

					let content2 = ""
					while (true) {
						let w = word[i] //新提取出来的词
						// console.log("lora  ", w)
						if (w == ":") { //中断符号
							i--; //中断后后退一位便于后续计算判断
							break
						} else {
							content2 += word[i]
						}
						if (i + 1 >= word.length) {
							break;
						}
						i++
					}
					word2 = content2
					list.push(REPLACE_KEY)
					// list.push(content2) //添加中断
				}

			} else {
				word2 = content
				list.push(REPLACE_KEY)
				// list.push(word2)
			}
		} else if (type == 2) { //代表后续的都是权重值
			let content = ""
			//单独存储一个
			list.push(c)
			while (true) {
				if (i + 1 >= word.length) {
					break;
				}
				i++
				let w = word[i] //新提取出来的词
				if (isText(w) || isSymbol(w, ".")) { //中断符号
					i--
					break
				} else {
					content += word[i]
				}

			}

			weight = content
			list.push(REPLACE_KEY_WEIGHT)
		}
		//  else if (type == 3) {
		// 	let content = ""
		// 	while (true) {
		// 		let w = word[i] //新提取出来的词
		// 		if (isSymbol(w, ["."]) || isText(w)) { //中断符号
		// 			i--; //中断后后退一位便于后续计算判断
		// 			break
		// 		} else {
		// 			content += word[i]
		// 		}
		// 		i++
		// 		if (i >= word.length) {
		// 			break;
		// 		}
		// 	}
		// 	list.push(content)
		// } 
		else if (type == 4) {
			list.push(c)
		}



	}
	let transfer = {
		list: list,
		origin: word, //原始值
		word: word2, //处理后的单独的词不含权重
		weight: weight, //权重
		isModel: isModel
	}

	// console.log(transfer)
	return transfer
}
export function replaceAll(input, toReplace, replaceWith) {
	if (input == null || input == undefined) {
		return input
	}
	// console.log(input)
	while (input.indexOf(toReplace) > -1) {
		input = input.replace(toReplace, replaceWith)
	}
	return input
}
// 中文符号替换成英文
export function replaceCharaterToComma(prompt) {
	prompt = replaceAll(prompt, '，', ",");
	prompt = replaceAll(prompt, '。', ",")
	prompt = replaceAll(prompt, '、', ",")
	prompt = replaceAll(prompt, '（', "(")
	prompt = replaceAll(prompt, '）', ")")
	prompt = replaceAll(prompt, '：', ":")
	prompt = replaceAll(prompt, "【", "[")
	prompt = replaceAll(prompt, "】", "]")
	return prompt;
}

/**判断是否是特殊符号
 * @param word 单个描述词*/
export function isSymbol(char, ignoreList) {
	// console.log("判断是否是符号", char)
	if (ignoreList != null && ignoreList != undefined) {
		for (var i = 0; i < ignoreList.length; i++) {
			if (ignoreList[i] == char) {
				// console.log("忽略此符号", char)
				return false
			}
		}
	}
	const specialCharRegex = /[\[\]!@#$%^&*()（）,.?":{}|<>]/;
	return specialCharRegex.test(char);
}
/**判断是否是普通文本
 * @param word 单个描述词*/
export function isText(char) {
	return !isNumeric(char) && !isSymbol(char)
}

/**判断是否是普通文本
 * @param word 单个描述词*/
export function isChinese(str) {
	var pattern = new RegExp("[\u4E00-\u9FA5]+");
	return pattern.test(str)
}
/**判断是否是普通文本
 * @param word 单个描述词*/
export function isEnglish(str) {
	var pattern = new RegExp("[A-Za-z]+");
	return pattern.test(str)
}


/**判断是否是数字
 * @param word 单个描述词*/
export function isNumeric(char) {
	var n = parseInt(char);
	return !isNaN(n)
}
/**判断是否是包含符号
 * @param word 单个描述词*/
export function isTextNotSymbol(word, ignoreList) {
	for (var i = 0; i < word.length; i++) {
		if (!isText(word[i]) || isSymbol(word[i], ignoreList)) {
			return false
		}
	}
	return true
}
/**判断是否是数字
 * @param word 单个描述词*/
export function isEmpty(char) {
	return char == null || char == undefined || char == "" || char.trim().length == 0
}

/**判断是否是数字
 * @param word 单个描述词*/
export function formatList2Str(list) {
	let result = ""
	for (var i = 0; i < list.length; i++) {
		result += list[i]
	}
	return result
}
/**transfer从list转换成word*/
export function transferToWord(transfer) {
	let REPLACE_KEY_WEIGHT = "WWWWWWWW";
	let REPLACE_KEY = "TTTTTTTT";
	let result = ""
	for (var i = 0; i < transfer.list.length; i++) {
		if (transfer.list[i] == REPLACE_KEY_WEIGHT) { //如果是权重值的话就直接操作增减
			result += transfer.weight
		} else if (transfer.list[i] == REPLACE_KEY) { //如果是权重值的话就直接操作增减
			result += transfer.word
		} else {
			result += transfer.list[i]
		}
	}
	return result
}

/**判断是否是数字
 * @param word 单个描述词*/
export function test(word) {
	let list = [];
	list.push("防静电死哦发久啊地方但是");
	// list.push("(防静电死哦发久啊地方但是)");
	// list.push("(112233)");
	// list.push("(112233:1.2)");
	// list.push("（防静电死哦发久啊地方但是）");
	// list.push("防静电死哦发久啊地方但是:1.2");
	// list.push("(防静电死哦发久啊地方但是:1.2)");
	// list.push("([一个大棒子_小棒子:1.2])");
	// list.push("((girl standing in the lake):1.25)");
	// list.push("girl:1.2 AND standing AND in the lake");
	// list.push("[cow|cow|horse|man|siberian tiger|ox|man] in a field");
	// list.push("girl-bot:1.2");
	// list.push("(路易斯·罗约:1.2):1.2");
	// list.push("<路易斯·罗约:1.2>");
	// list.push("<lora:QingNvZhuang:0.5>");
	// list.push("animeLineartMangaLike_v30MangaLike:1")
	// list.push("<lora:animeLineartMangaLike_v30MangaLike:1>");
	// list.push("<lyco:dis_V1-000020:0.5>");
	// list.push("<lyco:[LoHa] Oboro:1.0>");
	// list.push(" <lora:84a3aab2efb44bf60d696a811c5e39d2b09ab8129276be0efc08394e7cdec386:1>");
	// list.push("<lyco:[LoHa] Oboro:1.0>");
	// list.push("<lora:20230525122544:0.8>");
	// list.push("<lora:A-mechaREN[color_theme,mecha musume, mechanical parts, robot joints,headgear,full armor]:1>");
	// list.push("<lora:A-mechaREN[color_theme,mecha musume, mechanical parts, robot joints,headgear,full armor]:1>");
	// list.push("monoha --niji 5 --ar 2:3");

	for (var i = 0; i < list.length; i++) {
		console.log(list[i])
		let transfer = getPromptTransfer(list[i])
		// console.log(transferToWord(transfer))
		console.log(transfer.word)

		// console.log(list[i])
		// let result = operateWordWeight(list[i], 1)
		// console.log(result)
	}


	// console.log(isText(" "))
	// console.log(isSymbol(" "))
	// console.log(isTextNotSymbol(" ", ["-", "_", "·"]))



}